user_0 = {
    'username': 'efermi',
    'first': 'enrico',
    'last': 'fermi'
}
#遍历字典
for key, value in user_0.items():
    print("\nKey: " + key)
    print("Value: " + value)

#遍历字典中的所有键
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python' 
}
for name in favorite_languages.keys():
    print(name.title())


friends = ['phil', 'sarah']
for name in favorite_languages.keys():
    print(name.title())
    if name in friends:
        print(" Hi " + name.title() + ", I see your favorite language is " + favorite_languages[name].title() + "!")

#keys()方法返回一个列表，其中包含字典中的所有键
if 'erin' not in favorite_languages.keys():
    print("Erin, please take our poll!")

#按特定顺序返回字典中的所有键
for name in sorted(favorite_languages.keys()):
    print(name.title() + ", thank you for taking the poll.")

#遍历字典中的所有值,使用values()方法,它返回一个值列表，而不包含任何键
print("The following languages have been mentioned:")
for language in favorite_languages.values():
    print(language.title())

#使用set()剔除重复项,set()函数创建一个集合，其中包含列表中所有独一无二的值
print("The following languages have been mentioned:")
for language in set(favorite_languages.values()):
    print(language.title())

#练习
rivers = {
    'nile': 'egypt',
    'amazon': 'brazil',
    'yangtze': 'china' 
}
for river, country in rivers.items():
    print("The " + river.title() + " runs through " + country.title() + ".")

print("\nThe following rivers are included in this data set:")
for river in rivers.keys():
    print(river.title())

print("\nThe following countries are included in this data set:")
for country in rivers.values():
    print(country.title())


#根据favorite_languages，创建一个应该会接受调查的人名单，其中有些人已包含在字典中，而其他人未包含在字典中
people = ['jen','sarah','edward','phil','eric','michael','jason']
for name in people:
    if name in favorite_languages.keys():
        print("Hi " + name.title() + ", thank you for taking the poll!")
    else:
        print("Hi " + name.title() + ", please take our poll!")

#嵌套
#字典列表
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
    print(alien)

#创建一个用于存储外星人的空列表
aliens = []
#创建30个绿色的外星人
for alien_number in range(30):
    new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
    aliens.append(new_alien)
#显示前5个外星人
for alien in aliens[:5]:
    print(alien)
print("...")
#显示创建了多少个外星人
print("Total number of aliens: " + str(len(aliens)))

#修改前三个外星人
for alien in aliens[:3]:
    if alien['color'] == 'green':
        alien['color'] = 'yellow'
        alien['speed'] = 'medium'
        alien['points'] = 10
#显示前5个外星人
for alien in aliens[:5]:
    print(alien)
print("...")

#在字典中存储列表
pizza = {
    'crust': 'thick',
    'toppings': ['mushrooms','extra cheese']
}
#概述所点的比萨
print("You ordered a " + pizza['crust'] + "-crust pizza " + "with the following toppings:")
for topping in pizza['toppings']:
    print("\t" + topping)

#在字典中存储字典
users = {
    'aeinstein': { 
        'first': 'albert',
        'last': 'einstein',
        'location': 'princeton'
    },
    'mcurie': {
        'first': 'marie',
        'last': 'curie',
        'location': 'paris'
    },
}

for username, user_info in users.items():
    print("\nUsername: " + username)
    full_name = user_info['first'] + " " + user_info['last']
    location = user_info['location']
    print("\tFull name: " + full_name.title())
    print("\tLocation: " + location.title())


#练习
#6-7
person_1 = {
    'first_name': 'wang',
    'last_name': 'xiaoming',
    'age': 20,
    'city': 'beijing'   
}

person_2 = {
    'first_name': 'li',
    'last_name': 'xiaohong',
    'age': 21,
    'city': 'shanghai' 
}

person = [person_1,person_2]
for person in person:
    print(person)

#6-8
dog = {
    'name': 'wangcai',
    'age': 3,
    'owner': 'xiaoming' 
}
cat = {
    'name': 'miaomiao',
    'age': 2,
    'owner': 'xiaohong' 
}
pets = [dog,cat]
for pet in pets:
    print(pet)

#6-9
favorite_places = {
    'xiaoming': ['beijing','shanghai','guangzhou'],
    'xiaohong': ['shenzhen','hangzhou','wuhan'],
    'xiaozhang': ['nanjing','tianjin','changsha']  
}

for name, places in favorite_places.items():
    print("\n" + name.title() + "'s favorite places are:")
    for place in places:
        print("\t" + place.title())

#6-11
cities = {
    'beijing': {
        'country': 'china',
        'population': '21.5',
        'fact': 'capital of china'
    },
    'shanghai': {
        'country': 'china',
        'population': '24',
        'fact': 'capital of shanghai'
    },
    'guangzhou': {
        'country': 'china',
        'population': '23',
        'fact': 'capital of guangzhou'
    } 
}
for city, city_info in cities.items():
    print("\n" + city.title() + ":")
    country = city_info['country']
    population = city_info['population']
    fact = city_info['fact']
    print("\tCountry: " + country.title())
    print("\tPopulation: " + population)
    print("\tFact: " + fact)

